<template>
  <div id="ConsoleMEM" style="width: 100%; height: 100%; background: #ffffff; text-align: center">
    <div ref="ConsoleMEM" :style="{ width:'100%',height:'100%' }"></div>
  </div>
</template>

<script>
import moment from 'moment/moment'
import * as echarts from "echarts";

export default {
  name: 'ConsoleMEM',
  data() {
    return {
      chart: null,
      option: {
        title: {
          show: true,
          text: '内存',
          left: 'center',
          top: 20,
        },
        grid: {
          show: true,
          right: '30px',
          containLabel: true,
        },
        xAxis: {
          type: 'time',
          // 如果需要，可以设置 min 为你的数据中的最小时间戳
          // min: 'dataMin', // 或者使用具体的时间戳
          boundaryGap: false, // 尝试移除 boundaryGap 或者设置为 false
          axisLabel: {
            formatter: (v) => {
              return moment(v).format('HH:mm:ss')
            },
            showMaxLabel: true,
            interval: 'auto',
            rotate: 45
          },
        },
        yAxis: {
          type: 'value',
          min: 0,
          max: 1,
          splitNumber: 6,
          position: 'left',
          silent: true,
          axisLabel: {
            formatter: (v) => {
              return v * 100 + '%'
            },
          },
        },
        tooltip: {
          trigger: 'axis',
          formatter: (data) => {
            console.log(data)
            return (moment(data[0].data[0]).format('HH:mm:ss') + '</br>' + data[0].marker +
                ' 使用：' + (data[0].data[1] * 100).toFixed(2) + '%')
          },
        },
        series: [{
          type: 'line',
          itemStyle: {
            color: '#409EFF',
          },
          areaStyle: {
            color: {
              type: 'linear',
              x: 0,
              y: 0,
              x2: 0,
              y2: 1,
              colorStops: [
                {
                  offset: 0,
                  color: '#50a3f8', // 0% 处的颜色
                },
                {
                  offset: 1,
                  color: '#69b0fa', // 100% 处的颜色
                },
              ],
              global: false, // 缺省为 false
            },
          },
          data: [],
        }],
      },
    }
  },
  mounted() {
    this.initChart();
    this.$nextTick((_) => {
      setTimeout(() => {
        this.chart.resize();
      }, 100)
    })
  },
  unmounted() {
  },
  methods: {
    setData: function (data) {
      this.option.series[0].data = data;
      this.chart.setOption(this.option);
    },
    initChart() {
      this.chart = markRaw(echarts.init(this.$refs.ConsoleMEM));
    }
  },
}
</script>
